package com.scorpio.appuser.service.impl;

import com.scorpio.appuser.domain.UserShopCartEntity;
import com.scorpio.appuser.dto.UserShopCartDTO;
import com.scorpio.appuser.mapper.UserShopCartMapper;
import com.scorpio.appuser.service.UserShopCartService;
import com.scorpio.appuser.vo.UpdateShopCartCheckVo;
import com.scorpio.appuser.vo.UpdateShopCartNumberVo;
import com.scorpio.appuser.vo.AddShopCartVo;
import com.scorpio.common.exception.CustomException;
import com.scorpio.common.service.impl.BaseServiceImpl;
import com.scorpio.goods.domain.GoodsEntity;
import com.scorpio.goods.domain.GoodsSpecEntity;
import com.scorpio.goods.enums.GoodsStatusEnum;
import com.scorpio.goods.service.GoodsService;
import com.scorpio.goods.service.GoodsSpecService;
import com.scorpio.redis.cachelock.RedisLock;
import com.scorpio.redis.cachelock.RedisLockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserShopCartServiceImpl extends BaseServiceImpl<UserShopCartMapper, UserShopCartEntity> implements UserShopCartService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsSpecService specService;

    @Autowired
    private RedisLock redisLock;


    @Override
    public UserShopCartEntity selectByUser(Long uid, Long id) {
        return baseDao.selectByUser(uid, id);
    }

    @Override
    public List<UserShopCartEntity> selectBysUser(Long uid, Long[] ids) {
        return baseDao.selectBysUser(uid, ids);
    }

    @Override
    public List<UserShopCartDTO> appList(Long uid) {
        List<UserShopCartDTO> list = baseDao.appList(uid);
        for (UserShopCartDTO dto : list) {
            if (IsDisbled(dto.getSpecNumber())) {
                dto.setIsDisbled("0");
                dto.setIsCheck("1");
            } else {
                dto.setIsDisbled("1");
            }
        }
        return list;
    }

    private boolean IsDisbled(String specNumber) {
        GoodsSpecEntity specEntity = specService.selectByNumber(specNumber);
        if (specEntity == null || !GoodsStatusEnum.Status0.getValue().equals(specEntity.getStatus())
                || specEntity.getInventoryTotal() <= 0) {
            return true;
        }
        GoodsEntity goodsEntity = goodsService.selectById(specEntity.getGoodsId());
        if (goodsEntity == null || !GoodsStatusEnum.Status0.getValue().equals(goodsEntity.getStatus())) {
            return true;
        }
        return false;
    }

    @Override
    public int addShopCart(AddShopCartVo vo) {
        String lockKey = "UserShopCartServiceImpl_ShopCart:" + vo.uid;
        if (redisLock.lock(lockKey, 5)) {
            try {
                GoodsSpecEntity specEntity = specService.selectByNumber(vo.getSpecNumber());
                if (specEntity == null || !GoodsStatusEnum.Status0.getValue().equals(specEntity.getStatus())) {
                    throw new CustomException("该商品不存在或已下架");
                }
                GoodsEntity goodsEntity = goodsService.selectById(specEntity.getGoodsId());
                if (goodsEntity == null || !GoodsStatusEnum.Status0.getValue().equals(goodsEntity.getStatus())) {
                    throw new CustomException("该商品不存在或已下架");
                }
                UserShopCartEntity entity = baseDao.selectBySpec(vo.uid, specEntity.getId());
                if (entity == null) {
                    entity = new UserShopCartEntity();
                }
                int total = entity.getAmountTotal() + vo.getTotal();
                if (total > 99) {
                    total = 99;
                }
                if (total <= 0) {
                    //负数则删除购物车
                    if (entity.getId() != null) {
                        return baseDao.deleteById(entity.getId());
                    } else {
                        return 1;
                    }
                }
                entity.setUid(vo.uid);
                entity.setGoodsId(goodsEntity.getId());
                entity.setGoodsName(goodsEntity.getGoodsName());
                entity.setSpecId(specEntity.getId());
                entity.setSpecName(specService.getSpecName(specEntity));
                entity.setSpecNumber(specEntity.getNumber());
                entity.setOriginalPrice(specEntity.getPresentPrice());
                entity.setAmountTotal(total);
                entity.setIsCheck("0");
                int count = 0;
                if (entity.getId() != null) {
                    count = baseDao.updateById(entity);
                } else {
                    count = baseDao.insert(entity);
                }
                return count;
            } finally {
                redisLock.unlock(lockKey);
            }
        }
        throw new RedisLockException();
    }

    @Override
    public int updateShopCartNumber(UpdateShopCartNumberVo vo) {
        String lockKey = "UserShopCartServiceImpl_ShopCart:" + vo.uid;
        if (redisLock.lock(lockKey, 5)) {
            try {
                UserShopCartEntity entity = baseDao.selectById(vo.getId());
                if (!vo.uid.equals(entity.getUid())) {
                    throw new CustomException("无效操作");
                }
                int total = vo.getTotal();
                if (total > 99) {
                    total = 99;
                }
                if (total <= 0) {
                    //负数则删除购物车
                    if (entity.getId() != null) {
                        return baseDao.deleteById(entity.getId());
                    } else {
                        return 1;
                    }
                }
                entity.setAmountTotal(total);
                int count = 0;
                if (entity.getId() != null) {
                    count = baseDao.updateById(entity);
                } else {
                    count = baseDao.insert(entity);
                }
                return count;
            } finally {
                redisLock.unlock(lockKey);
            }
        }
        throw new RedisLockException();
    }

    @Override
    public int updateShopCartCheck(UpdateShopCartCheckVo vo) {
        return baseDao.updateShopCartCheck(vo.uid, vo.getId(), "0".equals(vo.getIsCheck()) ? "0" : "1");
    }

    @Override
    public int deleteShopCart(Long uid, Long[] ids) {
        return baseDao.deleteShopCart(uid, ids);
    }

    @Override
    public int clearShopCart(Long uid) {
        return baseDao.clearShopCart(uid);
    }

    @Override
    public int shopCartTotal(Long uid) {
        Integer total = baseDao.shopCartTotal(uid);
        return total == null ? 0 : total;
    }

}
